<?php

class Controller_Admin_Template extends Controller_Template {
	public $template = 'admin/template';
	
	public $auth_required = array('admin');
	
	public function before() {
		parent::before();
		
		if($this->request->is_ajax()) {
			$this->auto_render = FALSE;
		}
		#Open session
		$this->session = Session::instance();
		
		#Check user auth role
		if(($this->auth_required !== FALSE && Auth::instance()->logged_in($this->auth_required) === FALSE))
		{
			if(Auth::instance()->logged_in()) {
				$this->request->redirect('noaccess');
			}
			else {
				Session::instance()->set('redirect_url', $this->request->uri());
				$this->request->redirect('user/login');
			}
			
			if($this->auto_render) {
				$categories = Model::factory('category')
					->where('parent_category_id', '=', 0)
					->find_all();
				
				$this->template->categories = $categories;
				
				$this->template->title = 'Title Placeholder';
				$this->template->content = 'Body Placeholder';
				$this->template->styles = array();
				$this->template->scripts = array();
			}
		}
		
		
		if($this->auto_render) {
			$categories = Model::factory('category')
				->where('parent_category_id', '=', 0)
				->find_all();
			
			$this->template->categories = $categories;
			
			$this->template->title = 'Title Placeholder';
			$this->template->content = 'Body Placeholder';
			$this->template->styles = array();
			$this->template->scripts = array();
		}

		
	}
	
	public function after()
	{
		/*
		if($this->auto_render == FALSE) {
			// We have ajax or internal request here
			$this->template = $this->content;
		}
		else {
			// We have regular http request for a page
			$this->template = View::factory($this->template)
				->set('content', $this->content);
		}
		*/
		
		/*
		if ($this->auto_render)
		{
			$styles = array(
					'media/css/screen.css' => 'screen, projection',
					'media/css/print.css' => 'print',
					'media/css/style.css' => 'screen',
			);
	
			$scripts = array(
					'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
			);
	
			$this->template->styles = array_merge( $this->template->styles, $styles );
			$this->template->scripts = array_merge( $this->template->scripts, $scripts );
		}
		*/
		parent::after();
	}
}